package cn.javass.dp.state.example2;

import java.util.HashMap;
import java.util.Map;

/**
 * 投票管理
 */
public class VoteManager {
    //记录用户投票结果
    private Map<String, String> mapVote = new HashMap<>();
    //记录用户投票次数
    private Map<String, Integer> mapVoteCount = new HashMap<>();

    public void vote(String user, String voteItem) {
        Integer oldVoteCount = mapVoteCount.get(user);
        if (oldVoteCount == null) {
            oldVoteCount = 0;
        }
        oldVoteCount = oldVoteCount + 1;
        mapVoteCount.put(user,oldVoteCount);

        if (oldVoteCount ==1) {
            mapVote.put(user, voteItem);
            System.out.println(" 投票成功");
        } else if (oldVoteCount > 1 && oldVoteCount < 5) {
            System.out.println(" 请不要重复投票");

        } else if (oldVoteCount >= 5 && oldVoteCount <= 8) {
            String s = mapVote.get(user);
            if (s != null) {
                mapVote.remove(user);
            }
            System.out.println("恶意刷票，禁止投票");
        } else if (oldVoteCount > 8) {
            System.out.println(" 您已经被拉黑");
        }
    }
}
